package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service(version = "1.0.0")
public class DashBoardApiImpl implements DashBoardApi {

    //查询用户
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 查询活跃用户数
     * @return
     */
    @Override
    public Integer queryActiveUser(Integer sd,Integer ed) {
        return queryLoginCount(sd, ed);
    }

    /**
     * 获取用户登录次数
     * @param
     * @return
     */
    @Override
    public Integer queryLoginCount(Integer day) {
        return queryLoginCount(day,day+1);
    }

    /**
     * 封装获取登录用户数
     * @param sd
     * @param ed
     * @return
     */
    private Integer queryLoginCount(int sd,int ed) {
        Query query = new Query(Criteria.where("logeType").is("/user/loginVerification"));
        List<Log> loginLogs = this.mongoTemplate.find(query, Log.class,"log");

        Set<Integer> activeUserSet= new HashSet<>();
        for (Log loginLog : loginLogs) {
            Long logTime = loginLog.getLogTime();
            if (logTime >= this.getTime(sd) && logTime <= this.getTime(ed)){
                activeUserSet.add(loginLog.getUserId());
            }
        }
        return activeUserSet.size();
    }


    /**
     * 查询活跃用户（根据Date查询）
     * @param sd
     * @param ed
     * @return
     */
    @Override
    public Integer queryActiveUser(Date sd, Date ed) {
        Long sdTime = sd.getTime();
        Long edTime = ed.getTime();
        Query query = Query.query(Criteria.where("logeType").is("/login/loginVerification"));
        List<Log> logs = this.mongoTemplate.find(query, Log.class,"log");

        Set<Integer> activeUserSet= new HashSet<>();
        for (Log loginLog : logs) {
            Long logTime = loginLog.getLogTime();
            if (logTime >= sdTime && logTime <= edTime){
                activeUserSet.add(loginLog.getUserId());
            }
        }
        return activeUserSet.size();
    }








    @Override
    public Integer queryActiveUser(List<Integer> ids, Long sd, Long ed) {
        Query query = Query.query(Criteria.where("logeType").is("/login/loginVerification")
                .and("userId").in(ids).and("logTime").gt(sd).lt(ed));
        long count = this.mongoTemplate.count(query, Log.class);
        return (int)count;
    }


    /**
     * 0 昨天的结束
     * 1 昨天的开始
     * 2 前天的开始
     * @param day
     * @return
     */
    private long getTime(int day) {
        Calendar yesterdayEnd = Calendar.getInstance();
        yesterdayEnd.set(Calendar.HOUR_OF_DAY, 0);
        yesterdayEnd.set(Calendar.MINUTE, 0);
        yesterdayEnd.set(Calendar.SECOND, 0);
        yesterdayEnd.add(Calendar.DATE, day);
        Date time = yesterdayEnd.getTime();

        return time.getTime();
    }
}
